<!DOCTYPE html>
<html>
	
	<head>
		<meta charset="utf-8">

		<!-- !Always force latest IE rendering engine (even in intranet) & Chrome Frame
		Remove this if you use the .htaccess -->
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

		<title>02_JavaScript_对象、函数</title>
		<meta name="description" content="">
		<meta name="author" content="chenyong">

		<meta name="viewport" content="width=device-width; initial-scale=1.0">

		<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
		<link rel="shortcut icon" href="/favicon.ico">
		<link rel="apple-touch-icon" href="/apple-touch-icon.png">
		
		<style type="text/css">
			
			
			body{
				margin:50px;
			}
			
			div{
				font-size:12px;
			}
			
			div.box{
				margin-top:30px;
			}
		
			
		</style>
		
		<script type="text/javascript">
			
			//创建对象，对象名使用大写
			function Person(name,age)
			{
				//this表明指向Person内部属性
				this.name = name;
				this.age = age;
				this.address = "深圳";
				
				//申明类方法
				this.toString = function(){
					var str = "[name:" + this.name + " age:" + this.age + " address:" + this.address + "]";
					return  str;
				};
				
				//如果没有使用this申明 变量会作为局部变量存在
				var a = 10;
			}
			
			function fn1(){
				var p1 = new Person("学友",18);
				alert("p1[name:" + p1.name + " age:" + p1.age + " address:" + p1.address + "]");
				alert("通过内部函数输出："+p1.toString());
			}
			
			
			function fn(){
				var d = "123";
				return "ret";
			}
			
			function fn2(){
				var a = fn;
				var b = fn();
				
				alert("var a = fn;\n" + a);
				
				alert("var b = fn();\n" + b);
			}
			
			function fn3(){
				var obj = new Person("学友",18);
				for(var a in obj){
					alert(a + "=" + obj[a]);
				}
			}
			
		</script>
	</head>
	
	<body>
		
		<h3>02_JavaScript_对象、函数</h3>
		
		<div class="box">
			<p><b>对象定义</b></p>
			<p>对于JavaScript而言，可以通过function来创建一个对象</p>
			<p>使用this关键字来定义内部对象</p>
			<p>对象内部函数也要通过this关键字来定义</p>
			<p>如果没有使用this申明 变量会作为局部变量存在 </p>
			<button onclick="fn1()">测试fn1()</button>
		</div>
		
		<div class="box">
			<p><b>函数</b></p>
			<p>通过function来申明函数</p>
			<p>可以return返回值</p>
			<p>var a = fn;</p>
			<p>表示可以通过a();来调用函数</p>
			<p>var b = fn();</p>
			<p>表示b等于fn();的返回值</p>
			<button onclick="fn2()">测试fn2()</button>
		</div>
		
		
		<div class="box">
			<p><b>对象的遍历</b></p>
			<p>通过for in 遍历对象中的所有属性</p>
			<button onclick="fn3()">测试fn3()</button>
		</div>
	</body>
	
</html>